var wave = wave || {};
wave.Callback = function(a, b) {
  this.callback_ = a;
  this.context_ = b || null
};
wave.Callback.prototype.invoke = function() {
  this.callback_ && this.callback_.apply(this.context_, arguments)
};
wave.Mode = {UNKNOWN:0, VIEW:1, EDIT:2, DIFF_ON_OPEN:3, PLAYBACK:4};
wave.API_PARAM_ = "wave";
wave.ID_PARAM_ = "waveId";
wave.id_ = null;
wave.viewer_ = null;
wave.host_ = null;
wave.participants_ = [];
wave.participantMap_ = {};
wave.participantCallback_ = new wave.Callback(null);
wave.state_ = null;
wave.stateCallback_ = new wave.Callback(null);
wave.privateState_ = null;
wave.privateStateCallback_ = new wave.Callback(null);
wave.mode_ = null;
wave.modeCallback_ = new wave.Callback(null);
wave.inWaveContainer_ = false;gadgets.window = gadgets.window || {};
(function() {
  function a(d, c) {
    var f = window.getComputedStyle(d, "");
    f = f.getPropertyValue(c);
    f.match(/^([0-9]+)/);
    return parseInt(RegExp.$1, 10)
  }
  var b;
  gadgets.window.adjustWidth = function(d) {
    var c = parseInt(d, 10);
    d = false;
    if(isNaN(c)) {
      d = true;
      var f = gadgets.window.getViewportDimensions().width, e = document.body, g = document.documentElement;
      if(document.compatMode === "CSS1Compat" && g.scrollWidth) {
        c = g.scrollWidth !== f ? g.scrollWidth : g.offsetWidth
      }else {
        if(navigator.userAgent.indexOf("AppleWebKit") >= 0) {
          f = 0;
          for(e = [document.body];e.length > 0;) {
            g = e.shift();
            g = g.childNodes;
            for(c = 0;c < g.length;c++) {
              var h = g[c];
              if(typeof h.offsetLeft !== "undefined" && typeof h.scrollWidth !== "undefined") {
                var i = h.offsetLeft + h.scrollWidth + a(h, "margin-right");
                f = Math.max(f, i)
              }
              e.push(h)
            }
          }
          c = f + a(document.body, "border-right") + a(document.body, "margin-right") + a(document.body, "padding-right")
        }else {
          if(e && g) {
            c = g.scrollWidth;
            h = g.offsetWidth;
            if(g.clientWidth !== h) {
              c = e.scrollWidth;
              h = e.offsetWidth
            }
            c = c > f ? c > h ? c : h : c < h ? c : h
          }
        }
      }
    }
    if(c !== b && !isNaN(c) && !(d && c === 0)) {
      b = c;
      gadgets.rpc.call(null, "setIframeWidth", null, c)
    }
  }
})();wave.Participant = function(a, b, d) {
  this.id_ = a || "";
  this.displayName_ = b || "";
  this.thumbnailUrl_ = d || ""
};
wave.Participant.prototype.getId = function() {
  return this.id_
};
wave.Participant.prototype.getDisplayName = function() {
  return this.displayName_
};
wave.Participant.prototype.getThumbnailUrl = function() {
  return this.thumbnailUrl_
};
wave.Participant.fromJson_ = function(a) {
  var b = new wave.Participant;
  b.id_ = a.id;
  b.displayName_ = a.displayName;
  b.thumbnailUrl_ = a.thumbnailUrl;
  return b
};wave.State = function(a) {
  this.setState_({});
  this.rpc_ = a === undefined ? "wave_gadget_state" : a
};
wave.State.prototype.get = function(a, b) {
  if(a in this.state_) {
    return this.state_[a]
  }
  return b === undefined ? null : b
};
wave.State.prototype.getKeys = function() {
  var a = [], b;
  for(var d in this.state_) {
    if(!d.match(/___$/)) {
      b = d;
      a.push(b)
    }
  }
  return a
};
wave.State.prototype.getMeta = function(a, b, d) {
  if(a in this.meta_) {
    a = this.meta_[a];
    if(b != undefined) {
      return a[b]
    }
    return a
  }
  return d === undefined ? null : d
};
wave.State.prototype.submitDelta = function(a) {
  gadgets.rpc.call(null, this.rpc_, null, a)
};
wave.State.prototype.submitValue = function(a, b) {
  var d = {};
  d[a] = b;
  this.submitDelta(d)
};
wave.State.prototype.reset = function() {
  var a = {}, b;
  for(var d in this.state_) {
    if(!d.match(/___$/)) {
      b = d;
      a[b] = null
    }
  }
  this.submitDelta(a)
};
wave.State.prototype.toString = function() {
  return wave.util.printJson(this.state_, true)
};
wave.State.prototype.setState_ = function(a) {
  this.state_ = {};
  this.meta_ = {};
  this.applyDelta_(a)
};
wave.State.prototype.calculateDelta_ = function(a) {
  var b = {}, d;
  for(var c in a) {
    if(!c.match(/___$/)) {
      d = c;
      var f = this.state_.hasOwnProperty(d);
      if(!f || this.state_[d] != a[d]) {
        b[d] = a[d]
      }
    }
  }
  for(var e in this.state_) {
    if(!e.match(/___$/)) {
      d = e;
      a.hasOwnProperty(d) || (b[d] = null)
    }
  }
  return b
};
wave.State.prototype.applyDelta_ = function(a) {
  this.state_ = this.state_ || {};
  var b;
  for(var d in a) {
    if(!d.match(/___$/)) {
      b = d;
      var c = a[b];
      if(c != null) {
        if(typeof c == "string" || typeof c == "number") {
          this.state_[b] = c
        }else {
          if(typeof c.charAt == "function") {
            this.state_[b] = c.valueOf()
          }else {
            this.meta_[b] = c;
            this.state_[b] = c.value
          }
        }
      }else {
        delete this.state_[b];
        delete this.meta_[b]
      }
    }
  }
};var tamings___ = tamings___ || [], caja___, ___;
tamings___.push(function(a) {
  function b(d, c) {
    var f = {apply:___.markFuncFreeze(function(e, g) {
      return ___.callPub(d, "apply", [c, g])
    })};
    return new wave.Callback(f, ___.USELESS)
  }
  ___.grantRead(wave, "Mode");
  b.prototype = wave.Callback.prototype;
  wave.Callback.prototype.constructor = b;
  ___.markCtor(b, Object, "Callback");
  ___.primFreeze(b);
  ___.tamesTo(wave.Callback, b);
  ___.handleGenericMethod(b.prototype, "invoke", function() {
    return ___.callPub(this.callback_, "apply", [___.tame(this.context_), Array.slice(arguments, 0)])
  });
  caja___.whitelistCtors([[wave, "Participant", Object], [wave, "State", Object]]);
  caja___.whitelistMeths([[wave.Participant, "getDisplayName"], [wave.Participant, "getId"], [wave.Participant, "getThumbnailUrl"], [wave.State, "get"], [wave.State, "getKeys"], [wave.State, "reset"], [wave.State, "submitDelta"], [wave.State, "submitValue"], [wave.State, "toString"]]);
  caja___.whitelistFuncs([[wave, "getHost"], [wave, "getMode"], [wave, "getParticipantById"], [wave, "getParticipants"], [wave, "getState"], [wave, "getTime"], [wave, "getViewer"], [wave, "isInWaveContainer"], [wave, "log"], [wave, "setModeCallback"], [wave, "setParticipantCallback"], [wave, "setStateCallback"], [wave.util, "printJson"]]);
  a.outers.wave = ___.tame(wave);
  ___.grantRead(a.outers, "wave")
});wave.util = wave.util || {};
wave.util.SPACES_ = "                                                 ";
wave.util.toSpaces_ = function(a) {
  return wave.util.SPACES_.substring(0, a * 2)
};
wave.util.isArray_ = function(a) {
  try {
    return a && typeof a.length == "number"
  }catch(b) {
    return false
  }
};
wave.util.printJson = function(a, b, d) {
  if(!a || typeof a.valueOf() != "object") {
    if(typeof a == "string") {
      return"'" + a + "'"
    }else {
      if(a instanceof Function) {
        return"[function]"
      }
    }
    return"" + a
  }
  var c = [], f = wave.util.isArray_(a), e = f ? "[]" : "{}", g = b ? "\n" : "", h = b ? " " : "", i = 0;
  d = d || 1;
  b || (d = 0);
  c.push(e.charAt(0));
  var j;
  for(var k in a) {
    if(!k.match(/___$/)) {
      j = k;
      var l = a[j];
      i++ > 0 && c.push(", ");
      if(!f) {
        c.push(g);
        c.push(wave.util.toSpaces_(d));
        c.push(j + ": ");
        c.push(h)
      }
      c.push(wave.util.printJson(l, b, d + 1))
    }
  }
  if(!f) {
    c.push(g);
    c.push(wave.util.toSpaces_(d - 1))
  }
  c.push(e.charAt(1));
  return c.join("")
};wave.checkWaveContainer_ = function() {
  var a = gadgets.util.getUrlParameters();
  wave.inWaveContainer_ = a.hasOwnProperty(wave.API_PARAM_) && a[wave.API_PARAM_];
  wave.id_ = a.hasOwnProperty(wave.ID_PARAM_) && a[wave.ID_PARAM_]
};
wave.isInWaveContainer = function() {
  return wave.inWaveContainer_
};
wave.receiveWaveParticipants_ = function(a) {
  wave.viewer_ = null;
  wave.host_ = null;
  wave.participants_ = [];
  wave.participantMap_ = {};
  var b = a.myId, d = a.authorId;
  a = a.participants;
  var c;
  for(var f in a) {
    if(!f.match(/___$/)) {
      c = f;
      var e = wave.Participant.fromJson_(a[c]);
      if(c == b) {
        wave.viewer_ = e
      }
      if(c == d) {
        wave.host_ = e
      }
      wave.participants_.push(e);
      wave.participantMap_[c] = e
    }
  }
  if(!wave.viewer_ && b) {
    e = new wave.Participant(b, b);
    wave.viewer_ = e;
    wave.participants_.push(e);
    wave.participantMap_[b] = e
  }
  wave.participantCallback_.invoke(wave.participants_)
};
wave.receiveState_ = function(a) {
  wave.state_ = wave.state_ || new wave.State("wave_gadget_state");
  var b = wave.state_.calculateDelta_(a);
  wave.state_.setState_(a);
  wave.stateCallback_.invoke(wave.state_, b)
};
wave.receiveServerTime_ = function(a) {
  wave._serverDelta = a - (new Date).getTime()
};
wave.receivePrivateState_ = function(a) {
  wave.privateState_ = wave.privateState_ || new wave.State("wave_private_gadget_state");
  var b = wave.privateState_.calculateDelta_(a);
  wave.privateState_.setState_(a);
  wave.privateStateCallback_.invoke(wave.privateState_, b)
};
wave.receiveStateDelta_ = function(a) {
  wave.state_ = wave.state_ || new wave.State("wave_gadget_state");
  wave.state_.applyDelta_(a);
  wave.stateCallback_.invoke(wave.state_, a)
};
wave.receivePrivateStateDelta_ = function(a) {
  wave.privateState_ = wave.privateState_ || new wave.State("wave_private_gadget_state");
  wave.privateState_.applyDelta_(a);
  wave.privateStateCallback_.invoke(wave.privateState_, a)
};
wave.receiveMode_ = function(a) {
  wave.mode_ = a || {};
  wave.modeCallback_.invoke(wave.getMode())
};
wave.getViewer = function() {
  return wave.viewer_
};
wave.getHost = function() {
  return wave.host_
};
wave.getParticipants = function() {
  return wave.participants_
};
wave.getParticipantById = function(a) {
  return wave.participantMap_[a]
};
wave.getState = function() {
  return wave.state_
};
wave.getPrivateState = function() {
  return wave.privateState_
};
wave.getMode = function() {
  if(wave.mode_) {
    var a = wave.mode_["${playback}"], b = wave.mode_["${edit}"];
    if(a != null && b != null) {
      return a == "1" ? wave.Mode.PLAYBACK : b == "1" ? wave.Mode.EDIT : wave.Mode.VIEW
    }
  }
  return wave.Mode.UNKNOWN
};
wave.isPlayback = function() {
  var a = wave.getMode();
  return a == wave.Mode.PLAYBACK || a == wave.Mode.UNKNOWN
};
wave.setStateCallback = function(a, b) {
  wave.stateCallback_ = new wave.Callback(a, b);
  wave.state_ && wave.stateCallback_.invoke(wave.state_, wave.state_.state_)
};
wave.setPrivateStateCallback = function(a, b) {
  wave.privateStateCallback_ = new wave.Callback(a, b);
  wave.privateState_ && wave.privateStateCallback_.invoke(wave.privateState_, wave.privateState_.state_)
};
wave.setParticipantCallback = function(a, b) {
  wave.participantCallback_ = new wave.Callback(a, b);
  wave.participants_ && wave.participantCallback_.invoke(wave.participants_)
};
wave.setModeCallback = function(a, b) {
  wave.modeCallback_ = new wave.Callback(a, b);
  wave.mode_ && wave.modeCallback_.invoke(wave.getMode())
};
wave.getTime = function() {
  return(new Date).getTime() + wave._serverDelta
};
wave.log = function(a) {
  gadgets.rpc.call(null, "wave_log", null, a || "")
};
wave.setSnippet = function(a) {
  gadgets.rpc.call(null, "set_snippet", null, a || "")
};
wave.getWaveId = function() {
  return wave.id_
};
wave.internalInit_ = function() {
  wave._serverDelta = 0;
  wave.checkWaveContainer_();
  if(wave.isInWaveContainer()) {
    gadgets.rpc.register("wave_gadget_mode", wave.receiveMode_);
    gadgets.rpc.register("wave_gadget_state", wave.receiveState_);
    gadgets.rpc.register("wave_private_gadget_state", wave.receivePrivateState_);
    gadgets.rpc.register("wave_private_state_delta", wave.receivePrivateStateDelta_);
    gadgets.rpc.register("wave_participants", wave.receiveWaveParticipants_);
    gadgets.rpc.register("wave_server_time", wave.receiveServerTime_);
    gadgets.rpc.register("wave_state_delta", wave.receiveStateDelta_);
    gadgets.rpc.call(null, "wave_enable", null, "1.1")
  }
};
(wave.init_ = function() {
  window.gadgets && gadgets.util.registerOnLoadHandler(function() {
    wave.internalInit_()
  })
})();if(typeof wave == "undefined") {
  wave = {}
}
if(typeof wave.ui == "undefined") {
  wave.ui = {}
}
wave.ui.BASE = "http://wave-api.appspot.com/public/";
wave.ui.cssLoaded = false;
wave.ui.loadCss = function() {
  if(!wave.ui.cssLoaded) {
    wave.ui.cssLoaded = true;
    var a = document.createElement("link");
    a.setAttribute("rel", "stylesheet");
    a.setAttribute("type", "text/css");
    a.setAttribute("href", wave.ui.BASE + "wave.ui.css");
    document.getElementsByTagName("head")[0].appendChild(a)
  }
};
wave.ui.makeButton = function(a) {
  wave.ui.loadCss();
  a.innerHTML = "<span>" + a.innerHTML + "</span>";
  a.className += " wavebutton"
};
wave.ui.makeDialog = function(a, b, d) {
  function c() {
    a.style.display = "none"
  }
  wave.ui.loadCss();
  var f = a.innerHTML;
  a.innerHTML = "";
  var e = document.createElement("div");
  e.className = "wavedialoghead";
  var g = document.createElement("span"), h = document.createElement("div");
  h.className = "wavedialogclose";
  h.onclick = d || c;
  g.appendChild(h);
  g.appendChild(document.createTextNode(b));
  e.appendChild(g);
  a.appendChild(e);
  b = document.createElement("div");
  b.className = "wavedialogbody";
  b.innerHTML = f;
  a.appendChild(b);
  a.className += " wavedialog"
};
wave.ui.makeFrame = function(a) {
  wave.ui.loadCss();
  a.innerHTML = '<div class="waveboxhead"><span>&nbsp;</span></div><div class="waveboxbody">' + a.innerHTML + "</div>";
  a.className += " wavebox"
};if(typeof wave == "undefined") {
  wave = {}
}
if(typeof wave.client == "undefined") {
  wave.client = {}
}
wave.client.loadHashTags = function(a, b) {
  var d = "", c;
  for(var f in a) {
    if(!f.match(/___$/)) {
      c = f;
      if(d != "") {
        d += ","
      }
      d += c;
      if(a[c]) {
        d += ":" + a[c]
      }
    }
  }
  b || (d += ",restored:wave:" + wave.getWaveId());
  alert(d);
  window.parent.location.hash = d
};
wave.client.openWave = function(a) {
  wave.client.loadHashTags({"restored:wave":a}, true)
};
wave.client.openSearch = function(a) {
  wave.client.loadHashTags({"restored:search":a})
};
wave.client.maximizeWavePanel = function() {
  wave.client.loadHashTags({"minimized:nav":"", "minimized:contact":"", "minimized:search":""})
};
